package com.lottery.system.controller;

import com.lottery.system.common.pojo.CommonResult;
import com.lottery.system.common.utils.JacksonUtil;
import com.lottery.system.controller.param.DrawPrizeParam;
import com.lottery.system.controller.param.WinningRecordParam;
import com.lottery.system.controller.result.WinningRecordResult;
import com.lottery.system.service.DrawPrizeService;
import com.lottery.system.service.dto.WinningRecordDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: xiaoxie
 * create: 2024-09-11 09:11
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.controller
 * description: 抽奖控制层
 */
@RestController
public class DrawPrizeController {
    private final static Logger logger = LoggerFactory.getLogger(DrawPrizeController.class);
    @Autowired
    private DrawPrizeService drawPrizeService;
    /**
     * @description: 抽奖结果
     * @author: xiaoxie
     * @date: 2024/9/16 20:44
     * @param: [param]
     * @return: com.lottery.system.common.pojo.CommonResult<java.lang.Boolean>
     **/
    @RequestMapping("/draw/prize")
    public CommonResult<Boolean> drawPrize(
           @Validated @RequestBody DrawPrizeParam param) {
        drawPrizeService.drawPrize(param);
        return CommonResult.success(true);
    }
    /**
     * @description: 中奖记录的展示,其中如果奖品id不为空就展示奖品维度的中奖记录,如果为空就展示活动维度的中奖记录
     * @author: xiaoxie
     * @date: 2024/9/16 19:02
     * @param: [param]
     * @return: com.lottery.system.common.pojo.CommonResult<WinningRecordResult>
     **/
    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordResult>> showWinningRecord(
            @Validated @RequestBody WinningRecordParam param) {
        logger.info("showWinningRecord,param:{}", JacksonUtil.writeValueAsString(param));
        List<WinningRecordDTO> winningRecordDTOList = drawPrizeService.getRecords(param);
        return CommonResult.success(
                convertWinningRecordResult(winningRecordDTOList));
    }

    private List<WinningRecordResult> convertWinningRecordResult(
            List<WinningRecordDTO> winningRecordDTOList) {
        if(CollectionUtils.isEmpty(winningRecordDTOList)) {
            logger.info("winningRecordDTOList is empty");
            return List.of();
        }
        return winningRecordDTOList.stream()
                                   .map(
                                        wDTO->{
                                            WinningRecordResult result = new WinningRecordResult();
                                            result.setWinnerId(wDTO.getWinnerId());
                                            result.setWinnerName(wDTO.getWinnerName());
                                            result.setPrizeName(wDTO.getPrizeName());
                                            result.setPrizeTier(wDTO.getPrizeTier().getMessage());
                                            result.setWinningTime(wDTO.getWinningTime());
                                            return result;
                                        }
                                   ).toList();
    }
}
